/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elicitor.core;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 *
 * @author dileepa
 */
public class Alogrithm {

    String realTimeFileLoc;
    
    Double angerCenData[] = new Double[]{-0.7471369507085157,0.7832607664796685,1.0732022867450504E-4,0.10089569808901057,0.10089592897182272,0.03559529495102729,0.011291900047125755,8.898823737756821E-5,2.8229750117814357E-5,73.46281135376641,149.1753906475036,308.4276360163974,183.66425413115599,211.64038704389816,248.21660882504767,217.73825651715615,55.1815257064704};
    Double disgustCenData[] = new Double[]{-0.5747350848477006,0.6012072052403536,1.0712507785336387E-4,0.0714276275441647,0.07142770705555132,0.017175913415943392,0.006011864968812061,4.293978353985851E-5,1.5029662422030153E-5,70.11226668911848,172.4673206760875,333.2265711116558,194.80181664311093,223.21250879714174,277.7981763354092,236.96782870723527,63.60527972124576};
    Double happyCenData[] = new Double[]{-0.6658201983097342,0.7081457691862132,1.0605476716056417E-4,0.08986160361984334,0.08986186343903466,0.023642768453072723,0.009358371332239915,5.910692113268178E-5,2.3395928330599772E-5,72.1619941219489,165.15807123280766,339.3023985086961,193.94092225745908,238.39207967233267,292.52071230440487,244.53266680469204,72.56815256427396};
    Double fearCenData[] = new Double[]{-0.49105223517991514,0.4969993342425339,1.0529291725873093E-4,0.06546220072163948,0.06546224727851097,0.014653315023802505,0.0056642693353641405,3.6633287559506256E-5,1.4160673338410356E-5,68.83281778317046,151.35930631720484,254.77477889883764,174.2360684462906,189.69415731320166,217.95484735438345,196.18743766886718,36.05043839111189};
    Double surpriseCenData[] = new Double[]{-0.5611552329395634,0.5877373399862852,1.2287862922746962E-4,0.07535823560531418,0.0753583415295508,0.01589690233633141,0.006411527638734477,3.974225584082858E-5,1.6028819096836198E-5,74.03272407322967,155.5510736926498,313.27222655839165,184.45412367803715,221.64371053921323,273.32429119574954,228.5791973703267,65.69068414295307};
    Double sadnessCenData[] = new Double[]{-0.36248965737719907,0.380878218744562,1.0936634249948211E-4,0.0434842041338764,0.04348404585334769,0.00719128258213125,0.0025378646972317445,1.7978206455328137E-5,6.344661743079358E-6,65.2426183292498,120.315734584401,215.51833495817684,140.87499786525825,157.77797777957696,183.63348553783425,162.15834338477788,33.928119392664435};
            
    double deviateFromAnger;
    double deviateFromDisgust;
    double deviateFromHappy;
    double deviateFromFear;
    double deviateFromSurprise;
    double deviateFromSadness;
    
    
    
    public Alogrithm(String string) {
        
        this.realTimeFileLoc = string;
        
    }
    
    public String detectEmotion(){
        
        Scanner scanner = null;
        File file = new File(realTimeFileLoc);
        List <Double>realtimeDataList = new ArrayList<Double>();
        String suitableEmotion = null;
        
        
        try {
 
             scanner = new Scanner(file);
 
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                String result[] = line.split(",");
                System.out.println(line);
                System.out.println(result[0]);
        if(result.length == 17){ 
            for (String s : result){
                        realtimeDataList.add(Double.parseDouble(s));

            }
        }
            }
            
            
           deviateFromAnger = calcuDistance(realtimeDataList, angerCenData);
           deviateFromDisgust = calcuDistance(realtimeDataList, disgustCenData);
           deviateFromFear = calcuDistance(realtimeDataList, fearCenData);
           deviateFromHappy = calcuDistance(realtimeDataList, happyCenData);
           deviateFromSadness = calcuDistance(realtimeDataList, sadnessCenData);
           deviateFromSurprise = calcuDistance(realtimeDataList, surpriseCenData);
           
           Double devation[] = new Double[]{deviateFromAnger,deviateFromDisgust,deviateFromFear,deviateFromHappy,deviateFromSadness,deviateFromSurprise};
            
           suitableEmotion = findminimum(devation);
            
        }
            
            catch (Exception e){
                System.err.println(e.getMessage());
            }
        finally{
            scanner.close();
        }
        
        return suitableEmotion;
       
    }

    private Double calcuDistance(List<Double> realtimeDataList, Double emotion[]) {
        
        double d=0;
        
        for (int i=0; i<17;i++){
            
           d = d + (realtimeDataList.get(i)-emotion[i])*(realtimeDataList.get(i)-emotion[i]); 
        }
        
        return d;
    }

    private String findminimum(Double[] devation) {
        int j = 0;
        
        String emotion = null;
        
        for (int i=0;i<devation.length;i++){
            System.out.println("devation....."+devation[i]);
            if(devation[i]<devation[j]){
                j=i;
            }
        }
        
        if (j==0){
            emotion= "ANGER";
        }else if(j==1){
            emotion= "DISGUST";
        }else if(j==2){
            emotion= "FEAR";
        }else if(j==3){
            emotion= "HAPPY";
        }else if(j==4){
            emotion= "SADNESS";
        }else if(j==5){
            emotion= "SURPRISE";
        }
        
        return emotion;

    }
    
    
    
    
    
}
